package com.mr.pay.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jfree.ui.action.ActionRadioButton;

import com.mr.pay.Const;
import com.mr.pay.domain.Cust;
import com.mr.pay.domain.CustLine;
import com.mr.pay.service.PayService;
import com.skysz.framework.action.AbstractBaseAction;
import com.skysz.framework.action.ActionResponse;
import com.skysz.framework.action.ResponseType;
import com.skysz.framework.page.PageHandler;
import com.skysz.framework.page.PageLoadMode;
import com.skysz.framework.plugin.dhtmlx.grid.Column;
import com.skysz.framework.plugin.dhtmlx.grid.formatter.MapColumnFormatter;
import com.skysz.framework.plugin.dhtmlx.grid.json.ArrayJsonGrid;
import com.skysz.framework.utils.CollectionUtils;

/**
 * 开单/买单请求处理类
 * @author liuxl
 * 2011-4-12 下午12:41:42
 */
public class PayAction extends AbstractBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4991098134150305128L;
	private PayService payService;
	public void setPayService(PayService payService) {
		this.payService = payService;
	}
	
	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	public String findAll() throws IOException {
		ArrayJsonGrid grid = new ArrayJsonGrid();
		grid.setIsNeedIndex(true);
		
		PageHandler handler = new PageHandler(grid);
		StringBuffer hql = new StringBuffer();
		hql.append("select ");
		hql.append(" from");
		hql.append(" where");
		hql.append(" and");

		handler.setHql(hql.toString());
		handler.load();
		handler.output();
		return null;
	}
	
	/**
	 * 根据手机号加载客户信息
	 * @return
	 * @throws IOException
	 */
	public Cust loadCustByPhone() throws IOException {
		String phone = this.getParameterNotNull("phone", String.class);
		Cust c = this.payService.loadCustByPhone(phone);
		if( c != null ) {
			if( c.getRemark() == null )
				c.setRemark("");
			if( c.getCode() == null ) 
				c.setCode("");
		}
		return c;
	}
	
	/**
	 * 查询某个客户所有有效的卡
	 * @return
	 * @throws IOException
	 */
	public String findCardList() throws IOException {
		String phone = this.getParameterNotNull("phone", String.class);
		ArrayJsonGrid grid = new ArrayJsonGrid();
		grid.setIsNeedIndex(true);
		grid.addColumn(1, new Column("l.cardType", new MapColumnFormatter(Const.CARD_TYPE_MAP)));
		
		PageHandler handler = new PageHandler(grid);
		StringBuffer hql = new StringBuffer();
		hql.append("select l.id,l.cardType,ca.name,l.balanceAmount,l.count,'',l.createTime,''");
		hql.append(" from CustLine l,Cust ct,Card ca");
		hql.append(" where l.cust.id = ct.id");
		hql.append(" and l.card.id = ca.id");
		hql.append(" and ct.phone = '").append(phone).append("'");
		hql.append(" and l.enable = ").append(Boolean.TRUE);
		
		hql.append(" order by l.cardType");

		handler.setHql(hql.toString());
		handler.load();
		handler.setLoadMode(PageLoadMode.All);
		handler.output();
		return null;
	}
	/**
	 * 加载附件列
	 * @return
	 * @throws IOException
	 */
	public ActionResponse findAttachHeaders() throws IOException {
		ActionResponse ar = new ActionResponse(ResponseType.NO_MSG);
		String phone = this.getParameterNotNull("phone", String.class);
		List<CustLine> cLineList = this.payService.findCustLineList(phone);
		List<String[]> headerList = null;
		
		if( CollectionUtils.isNotEmpty(cLineList)) {
			headerList = new ArrayList<String[]>();
			String[] strs = null;
			for(CustLine l : cLineList ) {
				strs = new String[2];
				strs[0] = l.getCard().getId().toString();
				strs[1] = l.getCard().getName();
				headerList.add(strs);
			}
		}
		
		ar.setObject(headerList);
		return ar;
	}
	
}
